package com.ruoyi.web.listener;

import com.bye.xiaomi_mall.domain.TdOrderDetail;
import com.bye.xiaomi_mall.mapper.TdGoodsMapper;
import com.bye.xiaomi_mall.mapper.TdOrderDetailMapper;
import com.bye.xiaomi_mall.mapper.TdShoppingCartMapper;
import com.bye.xiaomi_mall.service.ITdShoppingCartService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class PayCallbackRabbitListener {
    @Autowired
    private TdOrderDetailMapper orderDetailMapper;
    @Autowired
    private TdGoodsMapper tdGoodsMapper;
    @Autowired
    private ITdShoppingCartService shoppingCartService;
    @Autowired
    private TdShoppingCartMapper tdShoppingCartMapper;
    //配置正常交换机
    /*
     * 增加销量
     * */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(name = "payback.fanout", type = ExchangeTypes.FANOUT),
            value = @Queue(name = "payback.fanout.queue1"),
            key = ""
    ))
    public void listenPayCallbackAddSales(String orderCode) {
        //根据订单号查询订单详情
        List<TdOrderDetail> list = orderDetailMapper.selectOrderDetail(orderCode);

        for (TdOrderDetail orderDetail : list) {
//            减少库存
            tdGoodsMapper.updateGoodsSales(orderDetail);
//            清除购物车
            tdShoppingCartMapper.deleteTdShoppingCartByGoodsId(orderDetail.getGoodsId());
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(name = "payback.fanout", type = ExchangeTypes.FANOUT),
            value = @Queue(name = "payback.fanout.queue2"),
            key = ""
    ))
    public void listenPayCallbackCreateDealLog(String orderCode) {

        System.out.println("交易流水生成");
    }
}
